package com.baox.agridata.synchronous.schedule;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baox.agridata.synchronous.constant.Constant;
import com.baox.agridata.synchronous.entity.quanguo.*;
import com.baox.agridata.synchronous.entity.shanxi.*;
import com.baox.agridata.synchronous.service.business.AsyncLogOperationService;
import com.baox.agridata.synchronous.service.quanguo.*;
import com.baox.agridata.synchronous.service.shanxi.*;
import com.baox.agridata.synchronous.util.DateUtil;
import com.baox.agridata.synchronous.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用来做数据分割使用
 */

@Component
@Slf4j
public class SplitDataSummary {


    @Autowired
    private JyzsdwbService jyzsdwbService;

    @Autowired
    private JyzsdwaService jyzsdwaService;


    @Autowired
    private JyzscpaService jyzscpaService;


    @Autowired
    private JyzscpbService jyzscpbService;

    @Autowired
    private JzyDdService jzyDdService;


    @Autowired
    private JzyDdsService jzyDdsService;

    @Autowired
    private McFyztxxsService mcFyztxxsService;


    @Autowired
    private McQyxxsService mcQyxxsService;


    @Autowired
    private McGrxxsService mcGrxxsService;


    @Autowired
    private ZsReturnsService zsReturnsService;

    @Autowired
    private TZsAbpService tZsAbpService;


    @Autowired
    private TCCOutAAService tccOutAAService;


    @Autowired
    private TCCOutPAService tccOutPAService;


    @Autowired
    private TCCAAService tccaaService;


    @Autowired
    private TCCPAService tccpaService;


    @Autowired
    private TBaseRegionService tBaseRegionService;


    @Autowired
    private TIMMUNEEarTagRecordService timmuneEarTagRecordService;


    @Autowired
    private AsyncLogOperationService asyncLogOperationService;


    @Value("${DbConfig.RI1}")
    private String RI1;


    private final String JYZ_LX0 = "0";  //作废状态
    private final String JYZ_LX1 = "1";
    private final String JYZ_LX2 = "2";
    private final String JYZ_LX3 = "3";

    //动物A、动物B的分批插入
    public Integer interceptListAnimal(List<Jyzsdwb> list, Integer type) {
        int listSize = list.size();
        int toIndex = 50;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 50) {
            if (i + 50 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<Jyzsdwb> newList = list.subList(i, i + toIndex);
            try {
                if (type.intValue() == 1) {
                    resultCount += jyzsdwaService.batchInsertJyzsdwa(newList);
                    asyncLogOperationService.batchInsertJyzsdwaBak(newList);  //异步插入备份数据
                } else if (type.intValue() == 2) {
                    resultCount += jyzsdwbService.batchInsertJyzsdwb(newList);
                    asyncLogOperationService.batchInsertJyzsdwbBak(newList);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultCount;
    }


    //产品A、产品B的分批插入
    public Integer interceptListProduct(List<Jyzscpb> list, Integer type) {
        int listSize = list.size();
        int toIndex = 100;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 100) {
            if (i + 100 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<Jyzscpb> newList = list.subList(i, i + toIndex);
            try {
                if (type.intValue() == 1) {
                    resultCount += jyzscpaService.batchInsertJyzscpa(newList);
                    asyncLogOperationService.batchInsertJyzscpaBak(newList);
                } else if (type.intValue() == 2) {
                    resultCount += jyzscpbService.batchInsertJyzscpb(newList);
                    asyncLogOperationService.batchInsertJyzscpbBak(newList);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return resultCount;
    }


    //外省动物A数据
    public Integer interceptListOutAA(List<TCCOutAAEntity> list) {
        int listSize = list.size();
        int toIndex = 100;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 100) {
            if (i + 100 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<TCCOutAAEntity> newList = list.subList(i, i + toIndex);
            resultCount += tccOutAAService.batchInsertTCCOutAA(newList);
        }
        return resultCount;
    }


    //外省产品A数据
    public Integer interceptListOutPA(List<TCCOutPAEntity> list) {
        int listSize = list.size();
        int toIndex = 100;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 100) {
            if (i + 100 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<TCCOutPAEntity> newList = list.subList(i, i + toIndex);
            resultCount = tccOutPAService.batchInsertTCCOutPA(newList); // 插入本地数据库
        }
        return resultCount;
    }


    /**
     * 操作动物证
     *
     * @param jyzdds
     */
    public Integer operationTCCOutAAData(Jyzdds jyzdds) {
        JSONObject _Status = new JSONObject();
        if (JYZ_LX0.equals(jyzdds.getLx())) {
            _Status.put("key", Constant.RECEIVED_RECEIVED);
            _Status.put("Name", "作废");
        } else if (JYZ_LX1.equals(jyzdds.getLx())) {
            _Status.put("key", Constant.RECEIVED_UN_RECEIVE);
            _Status.put("Name", "未回收");
        } else if (JYZ_LX2.equals(jyzdds.getLx())) {
            _Status.put("key", Constant.RECEIVED_STATUS);
            _Status.put("Name", "已回收");
        }
        //1.先判断出来是入省的证，还是本省开出去的证
        Integer regionCount = tBaseRegionService.selectCountBySixRegionCode(jyzdds.getJyzdqhdm(), RI1); //到达地区划
        if (regionCount != null && regionCount.intValue() > 0) { //说明数据是入省的数据，因为目的地区划在陕西查到了，也就是对应的是T_CC_OUT_AA
            TCCOutAAEntity ccOutAA = tccOutAAService.selectTCCOutAAByMidOrUUID(jyzdds.getJyzid());
            if (ccOutAA != null && !JYZ_LX3.equals(jyzdds.getLx()) && !JYZ_LX2.equals(jyzdds.getLx())) {  //这里只能处理0和1的情况 作废和未到达的情况
                tccOutAAService.updateTccOutAAStatus(_Status.toJSONString(), DateUtil.dateToString(new Date()), ccOutAA.getMid());  //外省入省的证只有作废、未回收状态，已回收状态是本省给的，在jyz_dds这个表里
            }
        } else { //本省开出来的A证，对应的就是T_CC_AA
            String jyzid = jyzdds.getJyzid();
            TCCAAEntity tccaaEntity = tccaaService.selectTCCAAInfoByMid(jyzid);
            if (tccaaEntity != null) {
                JSONObject jsonObj = (JSONObject) JSON.parse(tccaaEntity.getStatus());
                Integer statusKey = jsonObj.getInteger("key");
                if (!JYZ_LX3.equals(jyzdds.getLx()) && !JYZ_LX0.equals(jyzdds.getLx()) && statusKey.intValue() != Constant.RECEIVED_RECEIVED.intValue()) { //本省开出来的A证只能处理到达的状态，前提需要判断本省是否自己主动作废了数据
                    tccaaService.updateTCCAAStatus(_Status.toJSONString(), DateUtil.dateToString(new Date()), tccaaEntity.getMid());
                }
            }
        }
        Integer dResult = jzyDdService.deleteJyzDdById(jyzdds.getId());
        return dResult;
    }


    /**
     * 操作产品证
     *
     * @param jyzdds
     */
    public Integer operationTCCOutPAData(Jyzdds jyzdds) {
        JSONObject _Status = new JSONObject();
        if (JYZ_LX0.equals(jyzdds.getLx())) {
            _Status.put("key", Constant.RECEIVED_RECEIVED);
            _Status.put("Name", "作废");
        } else if (JYZ_LX1.equals(jyzdds.getLx())) {
            _Status.put("key", Constant.RECEIVED_UN_RECEIVE);
            _Status.put("Name", "未回收");
        } else if (JYZ_LX2.equals(jyzdds.getLx())) {
            _Status.put("key", Constant.RECEIVED_STATUS);
            _Status.put("Name", "已回收");
        }

        //1.先判断出来是入省的证，还是本省开出去的证
        Integer regionCount = tBaseRegionService.selectCountBySixRegionCode(jyzdds.getJyzdqhdm(), RI1);
        if (regionCount != null && regionCount.intValue() > 0) { //说明数据是入省的数据，因为目的地区划在陕西查到了，也就是对应的是T_CC_OUT_PA
            TCCOutPAEntity ccOutPa = tccOutPAService.selectTCCOutPaByMidOrUUID(jyzdds.getJyzid());
            if (ccOutPa != null && !JYZ_LX3.equals(jyzdds.getLx()) && !JYZ_LX2.equals(jyzdds.getLx())) {
                tccOutPAService.updateTccOutPAStatus(_Status.toJSONString(), DateUtil.dateToString(new Date()), ccOutPa.getMid());
            }
        } else {
            TCCPAEntity tccpaEntity = tccpaService.selectProductAByMid(jyzdds.getJyzid());
            if (tccpaEntity != null) {
                JSONObject jsonObj = (JSONObject) JSON.parse(tccpaEntity.getStatusJson());
                Integer statusKey = jsonObj.getInteger("key");
                if (!JYZ_LX3.equals(jyzdds.getLx()) && !JYZ_LX0.equals(jyzdds.getLx()) && statusKey.intValue() != Constant.RECEIVED_RECEIVED.intValue()) {  //本省开出来的A证只能处理到达的状态，前提需要判断本省是否自己主动作废了数据
                    tccpaService.updateTCCPAInfo(_Status.toJSONString(), tccpaEntity.getMid(), _Status.getInteger("key"));
                }
            }
        }
        Integer dResult = jzyDdService.deleteJyzDdById(jyzdds.getId());
        return dResult;
    }


    //全国检疫证到达本省发送表数据
    public Integer interceptListArriveCertificate(List<Jyzdds> list) {
        int listSize = list.size();
        int toIndex = 50;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 50) {
            if (i + 50 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<Jyzdds> newList = list.subList(i, i + toIndex);
            resultCount += jzyDdsService.batchInsertJyzDds(newList);
        }
        return resultCount;
    }


    /**
     * 切割本省贩运主体
     *
     * @param list
     * @return
     */
    public Integer interceptListMcFyztxxs(List<Mcfyztxxs> list) {
        int listSize = list.size();
        int toIndex = 100;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 100) {
            if (i + 100 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<Mcfyztxxs> newList = list.subList(i, i + toIndex);
            resultCount += mcFyztxxsService.batchInsertMcFyztxxs(newList);
        }
        return resultCount;
    }


    /**
     * 切割本省养殖企业的数据
     *
     * @param list
     * @return
     */
    public Integer interceptListFarmCompany(List<McQyxxs> list) {
        int listSize = list.size();
        int toIndex = 50;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 50) {
            if (i + 50 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<McQyxxs> newList = list.subList(i, i + toIndex);
            resultCount += mcQyxxsService.batchInsertMcQyxxs(newList);
        }
        return resultCount;
    }


    /**
     * 切割本省养殖企业的数据
     *
     * @param list
     * @return
     */
    public Integer interceptListFarmPerson(List<McGrxxs> list) {
        int listSize = list.size();
        int toIndex = 50;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 50) {
            if (i + 50 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<McGrxxs> newList = list.subList(i, i + toIndex);
            resultCount += mcGrxxsService.batchInsertMcGrxxs(newList);
        }
        return resultCount;
    }


    /**
     * 切割本省养殖企业的数据
     *
     * @param list
     * @return
     */
    public Integer interceptListZsReturn(List<ZsReturns> list) {
        int listSize = list.size();
        int toIndex = 50;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 50) {
            if (i + 50 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<ZsReturns> newList = list.subList(i, i + toIndex);
            resultCount += zsReturnsService.batchInsertZsReturn(newList);
        }
        return resultCount;
    }


    /**
     * 切割耳标数据
     *
     * @param list
     * @return
     */
    public Integer interceptListTIMMUNEEarTagRecord(List<TIMMUNEEarTagRecord> list) {
        int listSize = list.size();
        int toIndex = 10;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 10) {
            if (i + 10 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<TIMMUNEEarTagRecord> newList = list.subList(i, i + toIndex);
            resultCount += timmuneEarTagRecordService.batchInsertIMMUNEEarTagRecord(newList);
        }
        return resultCount;
    }



    /**
     * 切割数据
     *
     * @param list
     * @return
     */
    public Integer interceptListTZSABPList(List<ZsAbp> list) {
        int listSize = list.size();
        int toIndex = 500;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 500) {
            if (i + 500 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<ZsAbp> newList = list.subList(i, i + toIndex);
            resultCount += tZsAbpService.batchInsertZsAbp(newList);
        }
        return resultCount;
    }

    @Autowired
    private ZsAbpService zsAbpService;


    /**
     * 切割数据
     *
     * @param list
     * @return
     */
    public Integer interceptListTZSABPListDelete(List<ZsAbp> list) {
        int listSize = list.size();
        int toIndex = 200;
        Integer resultCount = 0;
        for (int i = 0; i < list.size(); i += 200) {
            if (i + 200 > listSize) {        //作用为toIndex最后没有toIndex条数据则剩余几条newList中就装几条
                toIndex = listSize - i;
            }
            List<ZsAbp> newList = list.subList(i, i + toIndex);
            resultCount += zsAbpService.deleteZsAbpList(newList);
        }
        return resultCount;
    }


}
